package scenic.study.arithmetic.stack;

import scenic.study.arithmetic.Algorithm;

import java.util.Iterator;

/**
 * Created by scenic on 2017/2/13.
 */
public class ArrayFixedCapacityStack implements StackInterface<String> {

    private String[] a;
    private int N;

    public ArrayFixedCapacityStack(int cap) {
        a = new String[cap];
    }

    public boolean isEmpty() {
        return N == 0;
    }

    public int size() {
        return N;
    }

    public String push(String item) {
        a[N] = item;
        N++;
        return item;
    }

    public String pop() {
        return a[--N];
    }


    @Override
    public Iterator<String> iterator() {
        return new MyIterator();
    }

    private class MyIterator implements Iterator<String> {
        int i = N;

        @Override
        public boolean hasNext() {
            return i > 0;
        }

        @Override
        public String next() {
            return a[--i];
        }
    }
}
